package com.gaogzhen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.model.ExceptionEnum;
import com.gaogzhen.model.RRException;
import com.gaogzhen.utils.SqlExecuteUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.mapper.CoinTypeMapper;
import com.gaogzhen.domain.CoinType;
import com.gaogzhen.service.CoinTypeService;
/**
* @description ${description}
* @author Administrator
* @date 2022-11-03 11:43
* @version 1.0
*/
@Service
public class CoinTypeServiceImpl extends ServiceImpl<CoinTypeMapper, CoinType> implements CoinTypeService{

    @Override
    public Page<CoinType> findByPage(Page<CoinType> page, String code) {
        // 按最后修改时间倒序查询
        page.addOrder(OrderItem.desc("last_update_time"));
        return page(page, new LambdaQueryWrapper<CoinType>()
                            .like(StringUtils.isNotEmpty(code), CoinType::getCode, code)
        );
    }

    @Override
    public void setStatus(Long id, Byte status) {
        SqlExecuteUtils.sqlExceptionHandler(
                this::update,
                new LambdaUpdateWrapper<CoinType>().eq(CoinType::getId, id)
                    .set(CoinType::getStatus, status),
                ExceptionEnum.COIN_TYPE_SET_STATUS_FAIL);

    }

    @Override
    public List<CoinType> findAll(Byte status) {
        return list(new LambdaQueryWrapper<CoinType>().eq(status!= null, CoinType::getStatus, status));
    }
}
